<?php

/**
 *
 * 菜单挂件
 * @version $Id:$
 *
 */

namespace Todo\Widget;
use Todo\Builder\FormBuilder;
use Think\Controller;

class SiteWidget extends Controller{
	
	/**
	 * 站点切换
	 */
	public function switcher(){
		$siteList = D('Todo/Cms/Site')->where(array('status'=>1))->select();
		$this->assign('siteList', $siteList);
		$this->display(APP_PATH. '../data/tpl/app/widget/site_switcher.html');
	}

	/**
	 * 栏目列表
	 * @param int $siteId 站点的ID
	 *
	 */
	public function category($siteId, $currentCate){
		$cateList = D('Todo/Cms/Category')->where(array('status'=>1, 'site_id'=>$siteId))->order('sort ASC')->select();
		$this->assign('cateList', $cateList);
		$this->display(APP_PATH. '../data/tpl/app/widget/site_category.html');
	}

	/**
	 * FORM表单页面展示模型的自定义字段
	 * @param $modelId
	 * @param $contentId
	 */
	public function fields($modelId, $contentId){
		$fields = D('Todo/Cms/Field')->where(array('model_id'=>$modelId))->order('sort')->select();
		if($fields) {
			if($contentId) {
				$vlist = D('Todo/Cms/Values')->where(array('content_id'=>$contentId))->select();
				$data = array();
				foreach ($vlist as $v){
					$data['field_'.$v['field_id']] = $v['values'];
				}
				$this->assign('data', $data);
			}
			$action = strtolower(ACTION_NAME);
			$defaults = include APP_PATH . '../data/fields.php';
			$fb = new FormBuilder();
			$fb->setType($action);
			foreach ($fields as $f) {
				$column = $defaults[$f['type']];
				if (!$column[$action]) continue;

				$column['list'] = false;
				$column['search'] = false;
				$column['title'] = $f['title'];
				//if ($action == 'add') $column['value'] = $f['defaults'];
				//else $column['value'] = '{$data.field_'.$f['id'].'}';

				$form = $fb->generateForm($column);
				$form['default'] = $f['defaults'];

				$fb->addItem('field_' . $f['id'], $column['type'], $column['title'], $form, $column['option']);
			}
			$this->show($fb->customer());
		}
	}
}